﻿using MediatR;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.SqlServer.Server;
using SmartCity.ProcessEngine.API.Domain;
using SmartCity.ProcessEngine.API.ErrorCode;
using SmartCity.ProcessEngine.API.Infrastructrue.Interfaces;
using SmartCity.ProcessEngine.API.Infrastructrue.Migrations;
using SmartCity.ProcessEngine.API.Read.Applications.Commands.Nodes;
using SmartCity.ProcessEngine.API.Read.DTOS;

namespace SmartCity.ProcessEngine.API.Read.Applications.CommandHandlers.Nodes
{
    /// <summary>
    /// 请求节点查询命令
    /// </summary>
    public class NodeQueryCommandHandler : IRequestHandler<NodeQueryCommand, APIResult<List<NodeDTO>>>
    {
        /// <summary>
        /// 节点仓储
        /// </summary>
        private readonly IBaseRespository<WF_Node> nodeRep;
        /// <summary>
        /// 表单仓储
        /// </summary>
        private readonly IBaseRespository<WF_Form> formRep;
        /// <summary>
        /// 表单分类仓储
        /// </summary>
        private readonly IBaseRespository<WF_FromType> typeRep;
        /// <summary>
        /// 行业仓储
        /// </summary>
        private readonly IBaseRespository<Industry> industryRep;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="nodeRep">节点仓储</param>
        /// <param name="formRep">表单仓储</param>
        /// <param name="typeRep">表单分类仓储</param>
        public NodeQueryCommandHandler(IBaseRespository<WF_Node> nodeRep,
            IBaseRespository<WF_Form> formRep,
            IBaseRespository<WF_FromType> typeRep,
            IBaseRespository<Industry> industryRep)
        {
            this.nodeRep = nodeRep;
            this.formRep = formRep;
            this.typeRep = typeRep;
            this.industryRep = industryRep;
        }

        /// <summary>
        /// 请求处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<APIResult<List<NodeDTO>>> Handle(NodeQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<NodeDTO>> result = new APIResult<List<NodeDTO>>();

            var list = from a in nodeRep.GetAll()
                       join b in formRep.GetAll() on a.FormNo equals b.FormNo
                       join c in typeRep.GetAll() on b.TypeNo equals c.TypeNo
                       join d in industryRep.GetAll() on c.IndustryId equals d.IndustryId
                       select new NodeDTO
                       {
                           NodeId = a.NodeId,
                           FormNo = b.FormNo,
                           NodeNo = a.NodeNo,
                           NodeName = a.NodeName,
                           FormName = b.FormName,
                           TypeNo=c.TypeNo,
                           TypeName=c.TypeName,
                           IndustryId=d.IndustryId,
                           IndustyName= d.IndustyName,
                       };
            if (request.IndustyId > 0)
            {
                list = list.Where(x => x.IndustryId == request.IndustyId);
            }
            if (!string.IsNullOrEmpty(request.TypeNo))
            {
                list = list.Where(x => x.TypeNo == request.TypeNo);
            }
            if (!string.IsNullOrEmpty(request.FormNo))
            {
                list=list.Where(x=>x.FormNo == request.FormNo);
            }


            result.Data=list.ToList();


            return Task.FromResult(result);
        }
    }
}
